package app.kotlin_1.utils;


import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by 刘智强 on 2017/10/20.
 * 根据 url下载网络文件
 */

public class HttpDownloader {
    private URL url;
    public static final int FILEEXIST = 1;//文件已经存在
    public static final int DOWNLOADFAIL = -1;//文件下载失败
    public static final int DOWNLOADSUCCESS = 0;//文件下载成功

    /**
     * 下载 text文本文件
     *
     * @param urlStr
     * @return
     */
    public String download(String urlStr) {
        StringBuffer sb = new StringBuffer("");
        String line = null;
        BufferedReader buffer = null;
        try {
            url = new URL(urlStr);
            HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
            buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
            while ((line = buffer.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        } finally {
            try {
                buffer.close();
            } catch (Exception e) {
                return sb.toString();
            }
        }
        return sb.toString();
    }

    /**
     * 下载文件
     *
     * @param urlStr   现在的文件的链接
     * @param path     下载的文件存放到SD的目录
     * @param fileName 文件名
     * @param cover    当文件存在时是否覆盖，true覆盖
     * @return
     */
    public int download(String urlStr, String path, String fileName, boolean cover) {
        InputStream inputStream = null;
        if (FileUtils.isFileExist(path + fileName) && !cover) {
            return FILEEXIST;
        }
        try {
            inputStream = getInputStreamFromUrl(urlStr);
            File resultFile = FileUtils.writData2SDFromInputStream(path, fileName, inputStream);
            if (resultFile == null) {
                return DOWNLOADFAIL;
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return DOWNLOADFAIL;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            //网络文件未找到
            return DOWNLOADFAIL;
        }
        try {
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return DOWNLOADSUCCESS;
    }

    /**
     * 根据url地址得到输入流
     *
     * @param urlStr
     * @throws MalformedURLException
     * @throws IOException
     */
    public InputStream getInputStreamFromUrl(String urlStr) throws IOException {
        HttpURLConnection urlConn = null;
        InputStream resInput = null;
        url = new URL(urlStr);
        urlConn = (HttpURLConnection) url.openConnection();
        resInput = urlConn.getInputStream();
        return resInput;
    }
}
